package com.donggua.aichebao.commonui.setting;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.donggua.aichebao.commonui.R;

import java.lang.ref.WeakReference;

/**
 * Created by fzh on 2017/8/21.
 */

public class DGSettingPhotoLayout extends RelativeLayout{
    private ImageView imgPhoto;
    private ImageView imgClose;
    private float density = 1;
    private Bitmap mBitmap;
    private boolean isClose = false;
    public DGSettingPhotoLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        init(context);
    }
    public DGSettingPhotoLayout(Context context) {
        super(context);
        init(context);
    }

    private  void init(Context context){
        density = context.getResources().getDisplayMetrics().density;
        imgPhoto = new ImageView(context);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        imgPhoto.setLayoutParams(layoutParams);
        this.addView(imgPhoto);
        imgClose = new ImageView(context);
        layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int w = dpToPx(20);
        layoutParams.width = w;
        layoutParams.height = w;
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
        imgClose.setLayoutParams(layoutParams);
        imgClose.setImageDrawable(getResources().getDrawable(R.mipmap.ic_launcher));
        this.addView(imgClose);
    }

    public ImageView getPhotoImageView(){
        return imgPhoto;
    }

    public ImageView getWeekPhotoImageView(){
        final WeakReference<ImageView> imageViewWeakReference = new WeakReference<ImageView>(imgPhoto);
        ImageView weekImage = imageViewWeakReference.get();
        return weekImage;
    }
    private int dpToPx(int dp){
        int px = (int)(dp*density+0.5);
        return px;
    }

    public void setImgPhoto(Bitmap bitmap){
        if(bitmap == null){
            return;
        }
        imgPhoto.setImageBitmap(null);
        if(mBitmap != null && !mBitmap.isRecycled()){
            mBitmap.recycle();
            mBitmap = null;
        }
        mBitmap = bitmap;
        imgPhoto.setImageBitmap(bitmap);
    }

    private void setClose(){
        imgClose.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                setVisibility(View.GONE);
            }
        });
    }

    public void setVisibility(int visibility){
        super.setVisibility(visibility);
        if(visibility == View.GONE){
            isClose = true;
        }else{
            isClose = false;
        }
    }
}
